import Vue from 'vue' // 引入vue
import VueRouter from 'vue-router'
Vue.use(VueRouter) // 挂载vueRouter插件

const routes = [
  {
    path:'/',redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('../views/login.vue'),
  },
  {
    path: '/common',
    component: () => import('../views/common.vue'),      
    children: [
      {
        path:'/',
        redirect: 'index',
      },
      {
        path: 'index',
        component: () => import('../views/index.vue'),
      },
      {
        path: 'content/:id',
        component: () => import('../views/content.vue'),
        props: router => ({ //props传参
          id: router.params.id //动态参数
        })
      },
      {
        path: 'message',
        component: () => import('../views/message.vue'),
      },
      {
        path: 'logInfo',
        component: () => import('../views/logInfo.vue'),
      },
      {
        path: 'opLog',
        component: () => import('../views/opLog.vue'),
      },
      {
        path: 'explain',
        component: () => import('../views/explain.vue'),
      },
      {
        path: 'role',
        component: () => import('../views/role.vue'),
      }        
    ],
    beforeEnter: (to, from, next) => {
      const token = sessionStorage.getItem('token');
      if(!token){
        return next('/login');
      }
      next()
    },   
  },    
  {
    path: '*', //404页面
    component: () => import("../views/N404/index.vue")
  }
]

const router = new VueRouter({
  linkActiveClass: "active", //定义路由激活类名
  base: process.env.BASE_URL,
  routes,
  //  scrollBehavior (){//滚动行为
  //    return {x:0,y:0}     
  //  }
})

router.beforeEach((to, from, next) => { //导航守卫
  document.getElementsByTagName("html")[0].classList.remove("ind_body"); //给html删除类名(该类名只在首页需要)
  window.scrollTo(0, 0); //切换路由，默认返回顶部
  next();
})

export default router